<template>
    <vol-box
        :title="title"
        v-model="dialogVisible"
        :width="volBoxWidth"
        :draggable="true"
        :on-model-close="closeDialog"
    >
        <template #content>
            <table-panel
                :height="tablePanelConfig.height"
                :isSelect="false"
                :loading="tablePanelConfig.loading"
                :tableData="tablePanelConfig.tableData"
                :columns="tablePanelConfig.columns"
                :pagination="false"
                :operationBtn="tablePanelConfig.operationBtn"
                :inputNumber="tablePanelConfig.inputNumber"
            />
        </template>
        <template #footer>
            <div>
                <el-button
                    @click="closeDialog"
                >
                    关闭
                </el-button>
                <el-button
                    v-if="tag !== 'look'"
                    type="primary"
                    @click="pastBtn"
                >
                    通过
                </el-button>
            </div>
        </template>
    </vol-box>
</template>
  
<script setup>
import { ref, computed } from 'vue'
import VolBox from '@/components/basic/VolBox.vue'
import http from '@/api/http'
import { ElMessage } from 'element-plus'
import TablePanel from '@/components/company/TablePanel.vue'
import { formatTime } from '@/uitils/formatTime'

// 变量
const tag = ref('')
const title = ref('')
const dialogVisible = ref(false)
const volBoxWidth = computed(() => {
    return 1120
})
const rowData = ref(null)
const tablePanelConfig = ref(
    {
        height: '300',
        loading: false,
        tableData: [],
        inputNumber: true,
        columns: [
            {
                label: '标题',
                prop: 'title'
            },
            {
                label: '55',
                prop: 'a55',
                editType: 'inputNumber'
            },
            {
                label: '60',
                prop: 'a60',
                editType: 'inputNumber'
            },
            {
                label: '65',
                prop: 'a65',
                editType: 'inputNumber'
            },
            {
                label: '70',
                prop: 'a70',
                editType: 'inputNumber'
            },
            {
                label: '75',
                prop: 'a75',
                editType: 'inputNumber'
            },
            {
                label: '80',
                prop: 'a80',
                editType: 'inputNumber'
            },
            {
                label: '小计',
                prop: 'total',
                formatter: (row) => {
                    if (row.title !== '比较') {
                        return row.a55 + row.a60 + row.a65 + row.a70 + row.a75 + row.a80
                    } else {
                        return row.total
                    }
                }
            },
            {
                label: '监察结果',
                prop: 'result'
            }
        ],
        operationBtn: [
            {
                dynamicName: (row) => {
                    if (row.title === '监察') {
                        return {
                            name: '确定',
                            color: ''
                        }
                    }
                    return {
                        name: '',
                        color: ''
                    }
                },
                click: (row) => {
                    checkBtn(row)
                }
            }
        ]
    }
)

// 函数
const emits = defineEmits(['success'])
defineExpose(
    {
        open
    }
)

function open(parameter) {
    title.value = parameter.title
    rowData.value = parameter.row
    if (parameter.tag === 'look') {
        tag.value = parameter.tag
        tablePanelConfig.value.inputNumber = false
        tablePanelConfig.value.operationBtn = []
    }
    init()
    dialogVisible.value = true
}

function closeDialog() {
    dialogVisible.value = false
}

function init() {
    tablePanelConfig.value.loading = true
    const params = {
        ExamineListId: rowData.value.ExamineListId
    }
    let tableDataTemp = []
    let type3 = {}
    http.post(`/api/ExamineComparison/detail`, params).then(res => {
        for (let i=0; i < res.length; i ++) {
            if (res[i].type === 1) {
                tableDataTemp.push(
                    {
                        title: '收花',
                        ...res[i],
                        total: res[i].a55 + res[i].a60 + res[i].a65 + res[i].a70 + res[i].a75 + res[i].a80
                    }
                )
            }
            if (res[i].type === 2) {
                tableDataTemp.push(
                    {
                        title: '整花',
                        ...res[i],
                        total: res[i].a55 + res[i].a60 + res[i].a65 + res[i].a70 + res[i].a75 + res[i].a80
                    }
                )
            }
            if (res[i].type === 3) {
                type3 = res[i]
            }
        }
        tableDataTemp.push(
            {
                title: '比较',
                a55: tableDataTemp[0].a55 === tableDataTemp[1].a55 ? '正确' : '错误',
                a60: tableDataTemp[0].a60 === tableDataTemp[1].a60 ? '正确' : '错误',
                a65: tableDataTemp[0].a65 === tableDataTemp[1].a65 ? '正确' : '错误',
                a70: tableDataTemp[0].a70 === tableDataTemp[1].a70 ? '正确' : '错误',
                a75: tableDataTemp[0].a75 === tableDataTemp[1].a75 ? '正确' : '错误',
                a80: tableDataTemp[0].a80 === tableDataTemp[1].a80 ? '正确' : '错误',
                total: tableDataTemp[0].total === tableDataTemp[1].total ? '正确' : '错误'
            },
            {
                editType: 'inputNumber',
                title: '监察',
                persionId: 3,
                ExamineListId: rowData.value.ExamineListId,
                type: 3,
                status: 0,
                a55: null,
                a60: null,
                a65: null,
                a70: null,
                a75: null,
                a80: null,
                total: null
            }
        )
        tablePanelConfig.value.tableData = tableDataTemp
        tablePanelConfig.value.loading = false
        for (const key in type3) {
            tablePanelConfig.value.tableData[3][key] = type3[key]
        }
        checkBtn(type3)
    }).catch(error => {
        console.log(error)
        tablePanelConfig.value.loading = false
    })
}

function checkBtn(row) {
    for (const key in row) {
        if (row.a55 === null || row.a60 === null || row.a65 === null || row.a70 === null || row.a75 === null || row.a80 === null) {
            ElMessage({
                message: '监察数据不能为空！',
                type: 'warning'
            })
            return
        }
    }
    // 监察数据对比收花数据
    let flowerCollectionData = tablePanelConfig.value.tableData
    if (row.a55 === flowerCollectionData[0].a55
        && row.a60 === flowerCollectionData[0].a60
        && row.a65 === flowerCollectionData[0].a65
        && row.a70 === flowerCollectionData[0].a70
        && row.a75 === flowerCollectionData[0].a75
        && row.a80 === flowerCollectionData[0].a80
    ) {
        tablePanelConfig.value.tableData[0].result = '正确'
    } else {
        tablePanelConfig.value.tableData[0].result = '错误'
    }
    // 监察数据对比整花花数据
    if (row.a55 === flowerCollectionData[1].a55
        && row.a60 === flowerCollectionData[1].a60
        && row.a65 === flowerCollectionData[1].a65
        && row.a70 === flowerCollectionData[1].a70
        && row.a75 === flowerCollectionData[1].a75
        && row.a80 === flowerCollectionData[1].a80
    ) {
        tablePanelConfig.value.tableData[1].result = '正确'
    } else {
        tablePanelConfig.value.tableData[1].result = '错误'
    }
    tablePanelConfig.value.inputNumber = false
    tablePanelConfig.value.operationBtn = []

    const params = {
        persionId: 3,
        ExamineListId: rowData.value.ExamineListId,
        type: 3,
        status: 0,
        a55: row.a55,
        a60: row.a60,
        a65: row.a65,
        a70: row.a70,
        a75: row.a75,
        a80: row.a80,
        createDate: formatTime(new Date())
    }
}

function pastBtn() {
    // 收花
    if (tablePanelConfig.value.tableData[0].result === '正确') {
        tablePanelConfig.value.tableData[0].status = 1
    } else {
        tablePanelConfig.value.tableData[0].status = 9
    }
    // 整花
    if (tablePanelConfig.value.tableData[1].result === '正确') {
        tablePanelConfig.value.tableData[1].status = 1
    } else {
        tablePanelConfig.value.tableData[1].status = 9
    }
    // 主数据
    if (tablePanelConfig.value.tableData[0].result === '错误' || tablePanelConfig.value.tableData[1].result === '错误') {
        rowData.value.status = 9
    } else {
        rowData.value.status = 1
    }
    const params = {
        Harvest: tablePanelConfig.value.tableData[0],
        Integration: tablePanelConfig.value.tableData[1],
        Comparsion: tablePanelConfig.value.tableData[3],
        MainData: rowData.value
    }
    http.post('/api/ExamineComparison/check', params).then(res => {
        ElMessage({
            message: '通过成功！',
            type: 'success'
        })
        closeDialog()
    }).catch(error => {
        console.log(error)
    })
}
</script>
  
<style lang="less" scoped>
</style>
